import request from '@/utils/request'
import type { ApiResponse, ApiListResponse } from '@/types/common'

// 法院信息类型定义
export interface CourtItem {
  id: number
  name: string
  level: string
  provinceid: number
  cityid: number
  distid: number
  address: string
  province_name?: string
  city_name?: string
  district_name?: string
  full_address?: string
  status: number
  create_time: number | string
  update_time: number | string
  delete_time: number
}

// 法院查询参数
export interface CourtQueryParams {
  page: number
  limit: number
  keywords?: string
  status?: string
}

// 法院表单数据
export interface CourtForm {
  id?: number
  court_name: string
  level: string
  province: string
  city: string
  district: string
  address: string
  status: number
}

// 法院列表响应数据
export interface CourtListResponse {
  list: CourtItem[]
  total: number
  page: number
  limit: number
}

// 法院管理相关API接口定义
export const courtApi = {
  // 获取法院列表
  getCourtList: (params: CourtQueryParams): Promise<ApiListResponse<CourtItem>> => {
    return request({
      url: '/api/lawcase/courts/list',
      method: 'get',
      params,
    })
  },

  // 获取单个法院信息
  getCourtDetail: (id: number): Promise<ApiResponse<CourtItem>> => {
    return request({
      url: '/api/lawcase/courts/view',
      method: 'get',
      params: { id },
    })
  },

  // 新增法院
  addCourt: (data: CourtForm): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/lawcase/courts/add',
      method: 'post',
      data,
    })
  },

  // 编辑法院
  editCourt: (data: CourtForm): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/lawcase/courts/edit',
      method: 'put',
      data,
    })
  },

  // 删除法院
  deleteCourt: (id: number): Promise<ApiResponse<any>> => {
    return request({
      url: '/api/lawcase/courts/del',
      method: 'delete',
      data: { id },
    })
  },

  // 获取启用的法院列表（用于下拉选择）
  getCourtSelectList: (): Promise<ApiResponse<CourtItem[]>> => {
    return request({
      url: '/api/lawcase/courts/set',
      method: 'get',
    })
  },
}
